package ltd.ninefish.framework.core.result;

import ltd.ninefish.framework.core.constant.NfConst;

import java.io.Serializable;

/**
 * @author zhaoxiubin
 * create-time 2023-12-11 17:07
 * description 响应式响应结果
 *
 * SpringWebflux 方式实现：
 * 异步非阻塞 方式，基于SpringWebflux+Reactor+Netty
 *
 **/

public class ResultFlux<T> implements Serializable{
    private static final long serialVersionUID = 5200818572467425282L;

    /**
     * 消息编码
     */
    private int code;
    /**
     * 消息内容
     */
    private String msg;
    /**
     * 响应数据
     */
    private T data;

    public static <T> ResultFlux<T> ok()
    {
        return restResult(null, NfConst.SUCCESS_FLUX, null);
    }

    public static <T> ResultFlux<T> ok(T data)
    {
        return restResult(data, NfConst.SUCCESS_FLUX, null);
    }

    public static <T> ResultFlux<T> ok(T data, String msg)
    {
        return restResult(data, NfConst.SUCCESS_FLUX, msg);
    }

    public static <T> ResultFlux<T> fail()
    {
        return restResult(null, NfConst.FAIL_FLUX, null);
    }

    public static <T> ResultFlux<T> fail(String msg)
    {
        return restResult(null, NfConst.FAIL_FLUX, msg);
    }

    public static <T> ResultFlux<T> fail(T data)
    {
        return restResult(data, NfConst.FAIL_FLUX, null);
    }

    public static <T> ResultFlux<T> fail(T data, String msg)
    {
        return restResult(data, NfConst.FAIL_FLUX, msg);
    }

    public static <T> ResultFlux<T> fail(int code, String msg)
    {
        return restResult(null, code, msg);
    }

    private static <T> ResultFlux<T> restResult(T data, int code, String msg)
    {
        ResultFlux<T> apiResult = new ResultFlux<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public static <T> Boolean isFail(ResultFlux<T> ret)
    {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(ResultFlux<T> ret)
    {
        return NfConst.SUCCESS_FLUX == ret.getCode();
    }

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }
}
